//
//  FloatingTabBarController.swift
//  gamlify
//
//  Created by Admin on 2024/12/8.
//

import UIKit
import SnapKit

enum TabBarItem: Int {
    case gamAi
    case discover
    case post
    case spectrum
    case settings
    
    var image: UIImage? {
        switch self {
        case .gamAi:
            return UIImage(named: "gamAi_normal")
        case .discover:
            return UIImage(named: "discover_normal")
        case .post:
            return UIImage(named: "post_normal")
        case .spectrum:
            return UIImage(named: "spectrum_normal")
        case .settings:
            return UIImage(named: "settings_normal")
        }
    }
    
    var highlightedImage: UIImage? {
        switch self {
        case .gamAi:
            return UIImage(named: "gamAi_highlighted")
        case .discover:
            return UIImage(named: "discover_highlighted")
        case .post:
            return UIImage(named: "post_normal")
        case .spectrum:
            return UIImage(named: "spectrum_highlighted")
        case .settings:
            return UIImage(named: "settings_highlighted")
        }
    }
}

class FloatingTabBarController: UITabBarController {
    
    private let floatingTabBar = UIView()
    private let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    private let centerButton = UIButton(type: .custom)
    private let tabbarItems: [TabBarItem] = [.gamAi, .discover, .post, .spectrum, .settings]
    
    var buttons: [UIButton] {
        floatingTabBar.subviews.filter { $0 != blurEffectView } as! [UIButton]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupFloatingTabBar()
    }
        
    private func setupFloatingTabBar() {
        // Hide the original tab bar
        tabBar.isHidden = true
        
        // Configure floating tab bar
        floatingTabBar.backgroundColor = .white.withAlphaComponent(0.7)
        floatingTabBar.layer.cornerRadius = 30
        
        // 设置border
        floatingTabBar.layer.borderWidth = 3
        floatingTabBar.layer.borderColor = UIColor.white.withAlphaComponent(0.1).cgColor
        
        floatingTabBar.layer.shadowColor = UIColor.black.withAlphaComponent(0.5).cgColor
        floatingTabBar.layer.shadowOpacity = 0.7
        floatingTabBar.layer.shadowOffset = CGSize(width: -2, height: 0)
        floatingTabBar.layer.shadowRadius = 20
        
        // Configure blur effect view
        blurEffectView.frame = floatingTabBar.bounds
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        blurEffectView.layer.cornerRadius = 30
        blurEffectView.clipsToBounds = true
        floatingTabBar.insertSubview(blurEffectView, at: 0)
        
        view.addSubview(floatingTabBar)
        floatingTabBar.translatesAutoresizingMaskIntoConstraints = false
        floatingTabBar.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalToSuperview().offset(-16)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-8)
            make.height.equalTo(62)
        }
        setupTabBarItems()
    }
        
    private func setupTabBarItems() {
        for (index, item) in tabbarItems.enumerated() {
            let button = UIButton(type: .custom)
            button.contentVerticalAlignment = .center
            button.setImage(item.image, for: .normal)
            button.tag = index
            button.addTarget(self, action: #selector(tabButtonTapped(_:)), for: .touchUpInside)
            // Cancel button's press effect
            button.adjustsImageWhenHighlighted = false
            button.showsTouchWhenHighlighted = false
            
            floatingTabBar.addSubview(button)
            button.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 第一个tab高亮
        updateTabBarAppearance(0)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let buttonWidth = 56
        let buttonHeight = 62
        let buttonMargin = (Int(floatingTabBar.frame.size.width) - 15 * 2 - buttonWidth * 5) / 4
        for (index, button) in buttons.enumerated() {
            button.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.leading.equalToSuperview().offset(15 + (buttonWidth + buttonMargin) * index)
                make.height.equalTo(buttonHeight)
                make.width.equalTo(buttonWidth)
            }
        }
    }
    
    
    @objc private func tabButtonTapped(_ sender: UIButton) {
        selectedIndex = sender.tag
        updateTabBarAppearance(sender.tag)
        self.delegate?.tabBarController?(self, didSelect: viewControllers![sender.tag])
#warning("TODO test")
        if let type = TabBarItem(rawValue: sender.tag) {
            switch type {
            case .gamAi:
                print("GamAI")
            case .discover:
                print("Discover")
            case .post:
                if let nav = viewControllers?[sender.tag] as? BaseNavigationController {
                    nav.pushViewController(LoginLaunchViewController(), animated: true)
                }
            case .spectrum:
                print("Spectrum")
            case .settings:
                print("Settings")
            }
        }
        
        
    }
    
    private func updateTabBarAppearance(_ index: Int) {
        for button in buttons {
            if button.tag == index {
                button.setImage(tabbarItems[button.tag].highlightedImage, for: .normal)
            } else {
                button.setImage(tabbarItems[button.tag].image, for: .normal)
            }
        }
    }
    
    func hideFloatingTabBar() {
        UIView.animate(withDuration: 0.3) {
            self.floatingTabBar.alpha = 0
        }
    }
    
    func showFloatingTabBar() {
        UIView.animate(withDuration: 0.3) {
            self.floatingTabBar.alpha = 1
        }
    }
}
